Package GUI

Source Code of GUI.RegistrationGUI$SaveChanges

package GUI;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import Background.Contestant;
import Background.Judge;
import Background.Lists;

/**
* A class to display the registration window in the gui.  Builds the window and displays it.
*
* @author Justin Sorrel
* @version 23 January, 2013
*
*/
@SuppressWarnings("serial")
public class RegistrationGUI extends JFrame
{
 
  /**
   * A dimension used in setting the size of the frame.
   */
 
  private static final Dimension MY_SIZE = new Dimension(1000, 500);

  /**
    * This field is for the Border Layout that contains all my buttons
    * for the information stage.
    */

  public Container my_info_panel;
 
  /**
    * This field creates a flow layout which hold the top buttons in the frame.
    */

  public Container my_grid;
 
  /**
   * A container to handle the flow layout.
   */
 
  public Container my_flow;
 
  /**
   * A text field for holding the first name.
   */
 
  public JTextField my_first_field;
 
  /**
   * A text field for holding the last name.
   */
 
  public JTextField my_last_field;
 
  /**
   * A text field for holding the email address.
   */
 
  public JTextField my_email_field;

  /**
   * A text field for holding the confirm email address.
   */
 
  public JTextField my_confirm_field;
 
  /**
   * A text field for holding the user's username.
   */
 
  public JTextField my_user_field;
 
  /**
   * A text field for holding the phone number.
   */
 
  public JTextField my_number_field;
 
  /**
   * A text field for holding the password.
   */
 
  public JPasswordField my_pass_field;
 
  /**
   * An int to hold the registration number for a contestant.
   */
 
  public static int registration_number;
 
  /**
   * A checkbox that the user can select to indicate they are a contestant.
   */
 
  private JCheckBox my_contestant;
 
  /**
   * A checkbox that the user can select to indicate they are a judge.
   */
 
  private JCheckBox my_judge;
 
  /**
   * A list to hold the strings of the user's information.
   */
 
  public ArrayList<String> my_list;
 
  /**
   * A JPanel for editing the user's information.
   */
 
  private JPanel my_edit_panel;

 
  /**
   * Constructor for the RegistrationGUI.  Sets the name of the window.
   */
 
  public RegistrationGUI() {
    
      super("REGISTRATION");
      my_flow = new JPanel(new FlowLayout());
      my_grid = new JPanel(new GridLayout(9,2));
      my_info_panel = new JPanel(new BorderLayout());
      my_edit_panel = new JPanel(new BorderLayout());
      my_list = new ArrayList<String>();
      my_first_field = new JTextField();
    my_last_field = new JTextField();
    my_email_field = new JTextField();
    my_confirm_field = new JTextField();
    my_user_field = new JTextField();
    my_number_field = new JTextField();
    my_pass_field = new JPasswordField();
    my_contestant = new JCheckBox("Contestant/Attendee");
    my_judge = new JCheckBox("Judge");
    }

  /**
   *   Method that starts the program and sets up the GUI.
   */
    public void start() {
      setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setVisible(true);
        setPreferredSize(MY_SIZE);
        createInfoGrid();
        add(my_info_panel);
        remove(my_edit_panel);
        validate();
        pack();
    }

    /**
     * A method to display the registration window after it's been closed.
     */
   
    public void displayRegistration(){
      setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setPreferredSize(MY_SIZE);
        add(my_info_panel);
        setVisible(true);
        pack();
       
    }
   
    /**
     * Creates the window for the information page.  This creates some labels and text
     * fields that will capture the user's entered data.
     */
   
  public void createInfoGrid() {
    final JButton submit = new JButton("Register Me!");
    submit.addActionListener(new SubmitListener());
    my_grid.add(new JLabel("First Name:"));
    my_grid.add(my_first_field);
    my_grid.add(new JLabel("Last Name:"));
    my_grid.add(my_last_field);
    my_grid.add(new JLabel("Email Address:"));
    my_grid.add(my_email_field);
    my_grid.add(new JLabel("Confirm Email:"));
    my_grid.add(my_confirm_field);
    my_grid.add(new JLabel("Phone Number:"));
    my_grid.add(my_number_field);
    my_grid.add(new JLabel("Username:"));
    my_grid.add(my_user_field);
    my_grid.add(new JLabel("Password:"));
    my_grid.add(my_pass_field);
    my_grid.add(new JLabel("Type of User:"));
    my_grid.add(new JLabel(""));
    my_grid.add(my_contestant);
    my_grid.add(my_judge);
    my_flow.add(my_grid, BorderLayout.CENTER);
    my_info_panel.add(my_flow);
    my_info_panel.add(submit, BorderLayout.SOUTH);
   
  }
  /**
   *
   * @param c the Contestant to retrieve information from.
   * @author Justin Wong
   */
  public void displayEdit(Contestant c){
    setVisible(true);
    createEditGrid(c);
    remove(my_info_panel);
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
      setPreferredSize(MY_SIZE);
      add(my_edit_panel);
      setVisible(true);
      pack();
     
  }
  /**
   *
   * @param c the Contestant to retrieve information from.
   * @author Justin Wong
   */
  private void createEditGrid(Contestant c){
    String name = c.getName();
    int space = name.indexOf(' ');
    String first_name = name.substring(0, space);
    String last_name = name.substring(space + 1, name.length());
   
    my_first_field.setText(first_name);
    my_last_field.setText(last_name);
    my_email_field.setText(c.getEmail());
    my_confirm_field.setText(c.getConfirm());
    my_number_field.setText(c.getPhoneNumber());
    my_user_field.setText(c.getUserName());
    my_pass_field.setText(c.getPassword());
   
    JButton save_changes = new JButton("Save Changes");
    save_changes.addActionListener(new SaveChanges(c));
    JPanel save_changes_panel = new JPanel(new BorderLayout());
    JPanel edit_gird = new JPanel(new GridLayout(9,2));
    JPanel edit_flow = new JPanel(new FlowLayout());
   
    edit_gird.add(new JLabel("First Name:"));
    edit_gird.add(my_first_field);
    edit_gird.add(new JLabel("Last Name:"));
    edit_gird.add(my_last_field);
    edit_gird.add(new JLabel("Email Address:"));
    edit_gird.add(my_email_field);
    edit_gird.add(new JLabel("Confirm Email:"));
    edit_gird.add(my_confirm_field);
    edit_gird.add(new JLabel("Phone Number:"));
    edit_gird.add(my_number_field);
    edit_gird.add(new JLabel("Username:"));
    edit_gird.add(my_user_field);
    edit_gird.add(new JLabel("Password:"));
    edit_gird.add(my_pass_field);
   
    save_changes_panel.add(save_changes, BorderLayout.EAST);
    edit_flow.add(edit_gird, BorderLayout.CENTER);
    my_edit_panel.add(save_changes_panel, BorderLayout.SOUTH);
    my_edit_panel.add(edit_flow);
   
   
  }
 
 
  /**
   * Method used to check whether or not the emails match.
   * @return True if emails match, otherwise false.
   */
 
  private boolean CheckEmail() {
    boolean check = false;
   
    String email =  my_email_field.getText();
    String confirm =  my_confirm_field.getText();
   
    if (email.equals(confirm)) {
        check = true;
    }
   
    return check;
 

  public void resetRegistration() {
    my_first_field.setText("");
    my_last_field.setText("");
    my_email_field.setText("");
    my_confirm_field.setText("");
    my_user_field.setText("");
    my_number_field.setText("");
    my_pass_field.setText("");
    my_contestant.setSelected(false);
    my_judge.setSelected(false);
         remove(my_edit_panel);
         remove(my_info_panel);
         validate();
        
   
  }

  /**
   * Inner class to go to the next window in the registration process when
   * button is pressed.
   * @author Justin Sorrell
   */
  class SubmitListener implements ActionListener {

    /**
     * Action performed method that displays the about message.
     * @param the_event The event, ignored.
     */
    @Override
    public void actionPerformed(final ActionEvent the_event) {
      boolean check = true;
     
      if(my_first_field.getText().equals("") || my_last_field.getText().equals("") ||
          my_email_field.getText().equals("") || my_confirm_field.getText().equals("") ||
          my_number_field.getText().equals("") || my_user_field.getText().equals("") ||
          my_pass_field.getPassword().equals("")){
        JOptionPane.showMessageDialog(my_info_panel, "One or more fields were left blank.");
     
     
      if(!my_contestant.isSelected() && !my_judge.isSelected()){
        check = false;
        JOptionPane.showMessageDialog(my_info_panel, "You must select a user type.");
      }
     
      if(my_contestant.isSelected() && my_judge.isSelected()){
        check = false;
        JOptionPane.showMessageDialog(my_info_panel, "You must select only one user type.");
      }
     
     
      if (CheckEmail() && check) {
        char[] temp;
        my_list.add(my_first_field.getText());
        my_list.add(my_last_field.getText());
        my_list.add(my_email_field.getText());
        my_list.add(my_confirm_field.getText());
        my_list.add(my_user_field.getText());
        temp = my_pass_field.getPassword();
        String password = new String(temp);
        my_list.add(password);
        my_list.add(my_number_field.getText());
        // for testing purposes
        System.out.println(my_list);
        if (my_contestant.isSelected()) {
          submitContestant();
          return;
        }
       
        if (my_judge.isSelected()){
          submitJudge();
          return;
        }
        remove(my_info_panel);
        validate();

      } else if (!CheckEmail()){
        JOptionPane.showMessageDialog(my_info_panel, "The given emails do not match each other.");
      }

    }


  }

  /**
   * This method handles the case when a contestant/attendee is registering.
   */
 
  private void submitContestant() {
    boolean valid = true;

    ArrayList<Contestant> contestantList = Lists.getContestantList();

    for (Contestant c : contestantList) {
      if (c.getUserName().equals(my_list.get(4))) {
        valid = false;
      }

    }

    if (valid) {
      String name = "";
      name = (my_list.get(0) + " " + my_list.get(1));
      Contestant c = new Contestant(name, my_list.get(2), my_list.get(3),
          my_list.get(4), my_list.get(5), (my_list.get(6)), registration_number);
      registration_number++;
      c.isContestant = true;
      RegistrationGUI r = Main_GUI.getRegistrationGUI();
      resetRegistration();
      JOptionPane.showMessageDialog(null,
          "Congratulations for registering! A contestant who does not make any " +
          "entries is considered to be an attendee only.");
      r.dispose();
     
      System.out.println("I am a contestant/attendee.");
      System.out.println("My name: " + c.getName());
      System.out.println("My email: " + c.getEmail());
      System.out.println("My phone number: " + c.getPhoneNumber());
      System.out.println("My registration number = " + c.getRegistrationNumber());

      Lists.saveData();

    }
   
    else{
      JOptionPane.showMessageDialog(my_info_panel, "A contestant has already registered with " +
          " this username");
    }
  }

 
  /**
   * This method handles the case when a judge is registering.
   */
 
  private void submitJudge() {
    boolean valid = true;

    ArrayList<Judge> judgeList = Lists.getJudgeList();

    for (Judge j : judgeList) {
      if (j.getUserName().equals(my_list.get(4))) {
        valid = false;
      }

    }

    if (valid) {
      String name = "";
      name = (my_list.get(0) + " " + my_list.get(1));
      Judge j = new Judge(name, my_list.get(2), my_list.get(3),
          my_list.get(4), my_list.get(5), (my_list.get(6)));
      RegistrationGUI r = Main_GUI.getRegistrationGUI();
      resetRegistration();
      JOptionPane.showMessageDialog(null,  "Congratulations for registering!");
      r.dispose();
      j.isContestant = false;
      System.out.println("I am a judge.");
      System.out.println("My name: " + j.getName());
      System.out.println("My email: " + j.getEmail());
      System.out.println("My phone number: " + j.getPhoneNumber());

      Lists.saveData();

    }
   
    else{
      JOptionPane.showMessageDialog(my_info_panel, "A judge has already registered with " +
          "this username");
    }
  }

 



 

 
 
  class SaveChanges implements ActionListener{
    private Contestant MY_CONTESTANT;
   
    public SaveChanges(Contestant c){
      MY_CONTESTANT = c;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
     
      String first_name = my_first_field.getText();
      String last_name = my_last_field.getText();
      String email = my_email_field.getText();
      String number = my_number_field.getText();
      String user_name = my_user_field.getText();
      char[] temp = my_pass_field.getPassword();
      String password = new String(temp);
     
      ArrayList<Contestant> contestant_list = Lists.getContestantList();
      boolean valid_user_name = true;
      for(int i = 0; i < contestant_list.size(); i++){
          Contestant c = contestant_list.get(i);
        String contestant_user_name = c.getUserName();
        if(contestant_user_name.equals(user_name)){
          valid_user_name = false;
          JOptionPane.showMessageDialog(null, "User name: " +
              user_name + "already exist");
          break;
        }
       
      }
      if(valid_user_name){
        MY_CONTESTANT.setName(first_name + " " + last_name);
        MY_CONTESTANT.setEmail(email);
        MY_CONTESTANT.setPhoneNumber(number);
        MY_CONTESTANT.setPassword(password);
        MY_CONTESTANT.setUserName(user_name);
        Lists.saveData();
        JOptionPane.showMessageDialog(null, "Changes are successful!");
     
    }
   
  }

}//close RegistrationGUI
TOP

Related Classes of GUI.RegistrationGUI$SaveChanges

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.